import Cookies from 'js-cookie'
import { swaggerApis } from '@/api/swagger'
import { constantRouterMap } from '@/router'
import Layout from '@/views/layout/Layout'
const app = {
  state: {
    sidebar: {
      opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
      withoutAnimation: false
    },
    device: 'desktop',
    language: Cookies.get('language') || 'en',
    size: Cookies.get('size') || 'medium',
    menus: {},
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      state.sidebar.opened = !state.sidebar.opened
      state.sidebar.withoutAnimation = false
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', 1)
      } else {
        Cookies.set('sidebarStatus', 0)
      }
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
      Cookies.set('sidebarStatus', 0)
      state.sidebar.opened = false
      state.sidebar.withoutAnimation = withoutAnimation
    },
    TOGGLE_DEVICE: (state, device) => {
      state.device = device
    },
    SET_LANGUAGE: (state, language) => {
      state.language = language
      Cookies.set('language', language)
    },
    SET_SIZE: (state, size) => {
      state.size = size
      Cookies.set('size', size)
    },
    MENUS: (state, menus) => {
      state.menus = menus
    },
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      // state.routers = constantRouterMap.concat(routers)
      // console.info(state.routers)
    }
  },
  actions: {
    toggleSideBar({ commit }) {
      commit('TOGGLE_SIDEBAR')
    },
    closeSideBar({ commit }, { withoutAnimation }) {
      commit('CLOSE_SIDEBAR', withoutAnimation)
    },
    toggleDevice({ commit }, device) {
      commit('TOGGLE_DEVICE', device)
    },
    setLanguage({ commit }, language) {
      commit('SET_LANGUAGE', language)
    },
    setSize({ commit }, size) {
      commit('SET_SIZE', size)
    },
    Menus({ commit }, menus) {
      return new Promise((resolve, reject) => {
        swaggerApis().then(data => {
          commit('MENUS', data)
          const routers = []
          console.info(data)
          data['tags'].forEach((item, i) => {
            const template = {
              path: `/${item.name}`,
              component: Layout,
              // redirect: '/swagger',
              name: item.description,
              key: item.name,
              meta: { title: item.description, key: item.name }
            }
            const childs = []
            for (const p in data['paths']) {
              // console.info(p)
              if (JSON.stringify(data['paths'][p]).indexOf(`"${item.name}"`) >= 0) {
                var srcReg = /\"summary\"\:[\'\"]?([^\'\"]*)[\'\"]?/i
                var arr = JSON.stringify(data['paths'][p]).match(srcReg)
                childs.push({
                  path: `doc/swagger?api=${p}&title=${arr[1]}`,
                  name: p,
                  meta: { title: arr[1] },
                  component: () => import(`@/views/swagger`)
                })
              }
            }
            if (childs.length > 0) {
              template.children = childs
            }
            routers.push(template)
          })

          commit('SET_ROUTERS', routers)
          // constantRouterMap
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    }

  }
}

export default app
